
package BusinessLogic;

import java.awt.Component;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;

public class StatusImage 
	extends DefaultListCellRenderer {

	private Map<Object, Icon> icons = null;
	
	public StatusImage(Map<Object, Icon> icons) {
		this.icons = icons;
	}
        
	//Hiển thị trạng thái bằng icon
	@Override
	public Component getListCellRendererComponent(
		JList list, Object value, int index, 
		boolean isSelected, boolean cellHasFocus) {
		
		String str = value.toString();
                String s1= str.substring(0,1);
                String s2=str.substring(1,str.length());
		JLabel name = 
			(JLabel) super.getListCellRendererComponent(list, 
				s2, index, isSelected, cellHasFocus);
		//icon hiển thị
                Icon icon = icons.get(s1);
		//hiển thị tên
		name.setIcon(icon);
		return name;
	}

        private static Icon icon(int i){
            switch(i){
                case(0):
                    return new ImageIcon(StatusImage.class.getResource("/Images/status_offline.png"));
                    
                case(1):
                    return new ImageIcon(StatusImage.class.getResource("/Images/status_online.png"));
                    
                case(2):
                    return new ImageIcon(StatusImage.class.getResource("/Images/status_busy.png"));
                    
                default:
                    return null;
                    
            }
        }
	public static void Image(JList list) {
		
		Map<Object, Icon> icons = new HashMap<Object, Icon>();
		icons.put("0", icon(0));
		icons.put("1", icon(1));
		icons.put("2", icon(2));
		list.setCellRenderer(new StatusImage(icons));
	}

}
